﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AR.ARBill;
using UFIDA.U9.AR.Receival;
using UFIDA.U9.Base.Doc;
using UFIDA.U9.CBO.FI.DTOs;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM;
using UFIDA.U9.PriceCal.PriceCalSV;
using UFIDA.U9.SM.Ship;
using UFIDA.U9.SM.ShipPlan;
using UFIDA.U9.SM.SMBE.Util;
using UFIDA.U9.SM.SOModify;
using UFIDA.U9.TI.SaleInvoice;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL.Engine;
using UFSoft.UBF.PL.Engine.Cache;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9C.Cust.ZL.BP
{
    internal class BusinessLogic
    {
        /// <summary>
        /// 更新出货单金额
        /// </summary>
        /// <param name="modifiedLines"></param>
        /// <returns></returns>
        public static List<EntityBatchResultData> AdjustmentShiplinePrice(IEnumerable<SOModifyLine> modifiedLines)
        {
            UFIDA.U9.CBO.SCM.Proxy.BatchOperationProxy bp = new UFIDA.U9.CBO.SCM.Proxy.BatchOperationProxy();
            bp.EntityTypeFullName = ShipLine.EntityRes.BE_FullName;
            bp.EntityAndFieldInfoList = new List<EntityAndFieldInfoData>();
            List<UIChangeInfoData> changeinfolist = new List<UIChangeInfoData>();
            foreach (var modifyLine in modifiedLines)
            {
                //SOLine soline = solines.Where(p => p.DocLineNo == modifyLine.DocLineNo).FirstOrDefault();

                if (decimal.Parse(modifyLine.NewValue) != decimal.Parse(modifyLine.OldValue))
                {
                    ShipPlanLine shipplan = ShipPlanLine.Finder.Find($"SrcDocLine={modifyLine.SOLine}");

                    if (shipplan != null && shipplan.ID > 0)
                    {
                        ShipLine shipline = ShipLine.Finder.Find($"SrcDocLineKey={shipplan.ID}");
                        if (shipline != null && shipline.ID > 0)
                        {


                            //shipline.FinallyPriceTC = decimal.Parse(modifyLine.NewValue);
                            //UIChangeInfoData changeinfo = shipline.GetShipLineChangeInfoData("FinallyPrice", modifyLine.NewValue, shipline.FinallyPriceTC);
                            //changeinfolist.Add(changeinfo);

                            EntityAndFieldInfoData entityfieldinfo = new EntityAndFieldInfoData();
                            entityfieldinfo.EntityInfo = new EntityInfoData();
                            entityfieldinfo.EntityInfo.ID = shipline.ID;
                            entityfieldinfo.EntityInfo.SysVer = shipline.SysVersion;
                            entityfieldinfo.FieldInfo = new List<BatchFieldAndValueData>();


                            if (shipline.IsPriceIncludeTax)
                            {
                                //最终价=单价
                                BatchFieldAndValueData field_price = new BatchFieldAndValueData();
                                field_price.FieldName = "FinallyPriceTC";
                                field_price.FieldValue = modifyLine.NewValue;
                                entityfieldinfo.FieldInfo.Add(field_price);
                                decimal totalamount = Convert.ToDecimal(modifyLine.NewValue) * shipline.QtyPriceAmount;


                                //价税合计
                                BatchFieldAndValueData field_TotalMoneyTC = new BatchFieldAndValueData();
                                field_TotalMoneyTC.FieldName = "TotalMoneyTC";
                                field_TotalMoneyTC.FieldValue = totalamount;
                                entityfieldinfo.FieldInfo.Add(field_TotalMoneyTC);

                                decimal netmonry = Decimal.Round(totalamount / (1 + shipline.TaxRate), 2);
                                decimal tax = totalamount - netmonry;

                                //未税
                                BatchFieldAndValueData field_TotalNetMoneyTC = new BatchFieldAndValueData();
                                field_TotalNetMoneyTC.FieldName = "TotalNetMoneyTC";
                                field_TotalNetMoneyTC.FieldValue = netmonry;
                                entityfieldinfo.FieldInfo.Add(field_TotalNetMoneyTC);




                                //税额
                                BatchFieldAndValueData field_TotalTaxTC = new BatchFieldAndValueData();
                                field_TotalTaxTC.FieldName = "TotalTaxTC";
                                field_TotalTaxTC.FieldValue = tax;
                                entityfieldinfo.FieldInfo.Add(field_TotalTaxTC);


                            }
                            else
                            {

                                //最终价
                                BatchFieldAndValueData field_price = new BatchFieldAndValueData();
                                field_price.FieldName = "FinallyPriceTC";
                                field_price.FieldValue = modifyLine.NewValue;
                                entityfieldinfo.FieldInfo.Add(field_price);
                                decimal totalamount = Convert.ToDecimal(modifyLine.NewValue) * shipline.QtyPriceAmount;

                                //价税合计
                                BatchFieldAndValueData field_TotalMoneyTC = new BatchFieldAndValueData();
                                field_TotalMoneyTC.FieldName = "TotalMoneyTC";
                                field_TotalMoneyTC.FieldValue = totalamount;
                                entityfieldinfo.FieldInfo.Add(field_TotalMoneyTC);

                                decimal netmonry = Decimal.Round(totalamount * shipline.TaxRate, 2);
                                decimal tax = totalamount - netmonry;

                                //未税
                                BatchFieldAndValueData field_TotalNetMoneyTC = new BatchFieldAndValueData();
                                field_TotalNetMoneyTC.FieldName = "TotalNetMoneyTC";
                                field_TotalNetMoneyTC.FieldValue = netmonry;
                                entityfieldinfo.FieldInfo.Add(field_TotalNetMoneyTC);

                                //税额
                                BatchFieldAndValueData field_TotalTaxTC = new BatchFieldAndValueData();
                                field_TotalTaxTC.FieldName = "TotalTaxTC";
                                field_TotalTaxTC.FieldValue = tax;
                                entityfieldinfo.FieldInfo.Add(field_TotalTaxTC);



                            }


                            bp.EntityAndFieldInfoList.Add(entityfieldinfo);
                        }
                    }

                    //ShipPreShipReCalculate.PerformCalculatePrice(changeinfolist, shipline.Ship, true);
                }
            }

            bp.IsParallel = false;
            if (bp.EntityAndFieldInfoList.Count > 0)
            {
                List<EntityBatchResultData> result = bp.Do();
                return result;
            }
            return null;
        }
       

        /// <summary>
        /// 根据销售订单行 红冲应收
        /// </summary>
        /// <param name="modifiedLines"></param>
        public static List<long> RedARBySOLine(IEnumerable<UFIDA.U9.SM.SO.SOLine> listModifiedSoline)
        {
            List<ARBillLine> ARBillLines = new List<ARBillLine>();
            //var listModifiedSoline = modifiedLines.Where(p => p.NewValue != p.OldValue);
            //通过销售变更单查出销售订单行
            //string shiplineids = string.Join(",", listModifiedSoline.Select(p => p.SOLine));
            string shiplineids = string.Join(",", listModifiedSoline.Select(p => p.ID));
            ////通过销售订单行查询出货单行
            List<ShipLine> shiplines = ShipLine.Finder.FindAll($"SOLinekey in ({shiplineids})").OfType<ShipLine>().ToList();
            if (shiplines == null || shiplines.Count <= 0) { return new List<long>(); }
            //根据出货单查询应收单行信息
            var arbills = ARBillLine.Finder.FindAll($"SrcBillLineID in ({string.Join(",", shiplines.Select(p => p.ID))})").OfType<ARBillLine>();
            if (arbills.Any())
            {
                //通过应收单行查询销售发票信息
                var saleInvoiceDocIndexs = SaleInvoiceDocIndex.Finder.FindAll($"SourceBillNo in ({string.Join(",", arbills.Select(p => "'" + p.ARBillHead.DocNo + "'"))})").OfType<SaleInvoiceDocIndex>();
                if (saleInvoiceDocIndexs.Any())
                    ARBillLines.AddRange(arbills.Where(p => !saleInvoiceDocIndexs.Select(t => t.SourceBillNo).Contains(p.ARBillHead.DocNo)));
                else
                    ARBillLines.AddRange(arbills);
            }

            //红冲应收
            //应收单红冲批操作
            U9.AR.ARBill.Proxy.ARBillRedFlushBatchBPProxy bp = new U9.AR.ARBill.Proxy.ARBillRedFlushBatchBPProxy();
            bp.ARBillHeadDTOs = new List<IDSysVersionDTOData>();
            //不等于 应收贷项通知单
            List<long> headList = ARBillLines.Select(p => p.ARBillHead).Where(p => p.BusinessType.Value != 1 && (p.RedFlushStatus == null || p.RedFlushStatus.Value == -1) && p.Cancel.Canceled == false).Select(p => p.ID).Distinct().ToList();
            bool isFirst = false;
            ARBillHead firstARBillHead = null;
            foreach (long headid in headList)
            {
                if (!isFirst)
                {
                    firstARBillHead = ARBillLines.Select(p => p.ARBillHead).Where(p => p.ID == headid).FirstOrDefault();
                    isFirst = true;
                }
                //headList.Add(headid);
                IDSysVersionDTOData idsys = new IDSysVersionDTOData();
                idsys.EntityID = headid;//应收单头ID
                bp.ARBillHeadDTOs.Add(idsys);//应收单头ID集合
            }
            if (bp.ARBillHeadDTOs.Count == 0)
            {
                return null;
            }

            bp.AccrueDate = firstARBillHead.AccrueDate;// accrueDate;//立账日期
            bp.PostPeriod = firstARBillHead.PostPeriod.ID;// postPeriod;//记账期间

            ARBillSubmitResultDTOData rstData = bp.Do();
            if (rstData.FailNum > 0)
            {
                throw new Exception($"红冲失败{string.Join("|", rstData.SubmitDetailDTO.Where(p => !string.IsNullOrEmpty(p.FailReason)).Select(p => p.DocNO + ":" + p.FailReason))}");
            }

            //成功            
            ARBillHead.EntityList redlist = ARBillHead.Finder.FindAll("RedFlushStatus=1 and BusinessType=1 and RedFlushBill in (0," + string.Join(",", headList) + ")");
            List<IDSysVersionDTOData> redar = new List<IDSysVersionDTOData>();
            foreach (ARBillHead item in redlist)
            {
                IDSysVersionDTOData idsys = new IDSysVersionDTOData();
                idsys.EntityID = item.ID;//应收单头ID                
                redar.Add(idsys);
            }
            //提交红冲应收
            UFIDA.U9.AR.ARBill.Proxy.ARBillSubmitBatchBPProxy smbp = new U9.AR.ARBill.Proxy.ARBillSubmitBatchBPProxy();
            smbp.ARBillHeadDTOs = redar;
            ARBillSubmitResultDTOData smres = smbp.Do();
            //审核红冲应收单
            UFIDA.U9.AR.ARBill.Proxy.ARBillApproveBatchBPProxy ckbp = new U9.AR.ARBill.Proxy.ARBillApproveBatchBPProxy();
            ckbp.ARBillHeadDTOs = redar;
            ARBillSubmitResultDTOData ckres = ckbp.Do();

            PLCacheManager.DataCache.FlushCache();
            PLCacheManager.ObjectCache.FlushCache();
            return headList;
        }

        /// <summary>
        /// 红冲应收
        /// </summary>
        /// <param name="modifiedLines"></param>
        public static List<long> RedAR(IEnumerable<SOModifyLine> modifiedLines)
        {
            List<ARBillLine> ARBillLines = new List<ARBillLine>();
            var listModifiedSoline = modifiedLines.Where(p => p.NewValue != p.OldValue);
            //通过销售变更单查出销售订单行
            string shiplineids = string.Join(",", listModifiedSoline.Select(p => p.SOLine));
            //通过销售订单行查询出货单行
            List<ShipLine> shiplines = ShipLine.Finder.FindAll($"SOLinekey in ({shiplineids})").OfType<ShipLine>().ToList();
            //根据出货单查询应收单行信息
            var arbills = ARBillLine.Finder.FindAll($"SrcBillLineID in ({string.Join(",", shiplines.Select(p => p.ID))})").OfType<ARBillLine>();
            if (arbills.Any())
            {
                //通过应收单行查询销售发票信息
                var saleInvoiceDocIndexs = SaleInvoiceDocIndex.Finder.FindAll($"SourceBillNo in ({string.Join(",", arbills.Select(p => "'" + p.ARBillHead.DocNo + "'"))})").OfType<SaleInvoiceDocIndex>();
                if (saleInvoiceDocIndexs.Any())
                    ARBillLines.AddRange(arbills.Where(p => !saleInvoiceDocIndexs.Select(t => t.SourceBillNo).Contains(p.ARBillHead.DocNo)));
                else
                    ARBillLines.AddRange(arbills);
            }

            //红冲应收
            //应收单红冲批操作
            U9.AR.ARBill.Proxy.ARBillRedFlushBatchBPProxy bp = new U9.AR.ARBill.Proxy.ARBillRedFlushBatchBPProxy();
            bp.ARBillHeadDTOs = new List<IDSysVersionDTOData>();
            //不等于 应收贷项通知单
            List<long> headList = ARBillLines.Select(p => p.ARBillHead).Where(p => p.BusinessType.Value != 1 && (p.RedFlushStatus == null || p.RedFlushStatus.Value == -1) && p.Cancel.Canceled == false).Select(p => p.ID).Distinct().ToList();
            bool isFirst = false;
            ARBillHead firstARBillHead = null;
            foreach (long headid in headList)
            {
                if (!isFirst)
                {
                    firstARBillHead = ARBillLines.Select(p => p.ARBillHead).Where(p => p.ID == headid).FirstOrDefault();
                    isFirst = true;
                }
                //headList.Add(headid);
                IDSysVersionDTOData idsys = new IDSysVersionDTOData();
                idsys.EntityID = headid;//应收单头ID
                bp.ARBillHeadDTOs.Add(idsys);//应收单头ID集合
            }
            if (bp.ARBillHeadDTOs.Count == 0)
            {
                return null;
            }

            bp.AccrueDate = firstARBillHead.AccrueDate;// accrueDate;//立账日期
            bp.PostPeriod = firstARBillHead.PostPeriod.ID;// postPeriod;//记账期间

            ARBillSubmitResultDTOData rstData = bp.Do();
            if (rstData.FailNum > 0)
            {
                throw new Exception($"红冲失败{string.Join("|", rstData.SubmitDetailDTO.Where(p => !string.IsNullOrEmpty(p.FailReason)).Select(p => p.DocNO + ":" + p.FailReason))}");
            }

            //成功            
            ARBillHead.EntityList redlist = ARBillHead.Finder.FindAll("RedFlushStatus=1 and BusinessType=1 and RedFlushBill in (0," + string.Join(",", headList) + ")");
            List<IDSysVersionDTOData> redar = new List<IDSysVersionDTOData>();
            foreach (ARBillHead item in redlist)
            {
                IDSysVersionDTOData idsys = new IDSysVersionDTOData();
                idsys.EntityID = item.ID;//应收单头ID                
                redar.Add(idsys);
            }
            //提交红冲应收
            UFIDA.U9.AR.ARBill.Proxy.ARBillSubmitBatchBPProxy smbp = new U9.AR.ARBill.Proxy.ARBillSubmitBatchBPProxy();
            smbp.ARBillHeadDTOs = redar;
            ARBillSubmitResultDTOData smres = smbp.Do();
            //审核红冲应收单
            UFIDA.U9.AR.ARBill.Proxy.ARBillApproveBatchBPProxy ckbp = new U9.AR.ARBill.Proxy.ARBillApproveBatchBPProxy();
            ckbp.ARBillHeadDTOs = redar;
            ARBillSubmitResultDTOData ckres = ckbp.Do();

            PLCacheManager.DataCache.FlushCache();
            PLCacheManager.ObjectCache.FlushCache();
            return headList;
        }


        /// <summary>
        ///  生成应收
        /// </summary>
        /// <param name="headList">红冲应收单ID</param>
        /// <param name="modifiedLines">销售变更单行信息</param>
        public static void AdjustmentAR(IEnumerable<long> headList, IEnumerable<SOModifyLine> modifiedLines)
        {
            //List<ARBillLine> ARBillLines = new List<ARBillLine>();
            var listModifiedSoline = modifiedLines.Where(p => p.NewValue != p.OldValue);
            string shiplineids = string.Join(",", listModifiedSoline.Select(p => p.SOLine));
            List<ShipLine> shiplines = ShipLine.Finder.FindAll($"SOLinekey in ({shiplineids})").OfType<ShipLine>().ToList();
            //var arbills = ARBillLine.Finder.FindAll($"SrcBillLineID in ({string.Join(",", shiplines.Select(p => p.ID))})").OfType<ARBillLine>();
            //if (arbills.Any())
            //{
            //    var saleInvoiceDocIndexs = SaleInvoiceDocIndex.Finder.FindAll($"SourceBillNo in ({string.Join(",", arbills.Select(p => "'" + p.ARBillHead.DocNo + "'"))})").OfType<SaleInvoiceDocIndex>();
            //    if (saleInvoiceDocIndexs.Any())
            //        ARBillLines.AddRange(arbills.Where(p => !saleInvoiceDocIndexs.Select(t => t.SourceBillNo).Contains(p.ARBillHead.DocNo)));
            //    else
            //        ARBillLines.AddRange(arbills);
            //}

            //生成新的应收单

            //var arbillheads = ARBillLines.Select(p => p.ARBillHead).Where(p => p.BusinessType.Value != 1 && (p.RedFlushStatus == null || p.RedFlushStatus.Value == -1) && p.Cancel.Canceled == false).Distinct<ARBillHead>();
            ARBillHead.EntityList arbillheads = ARBillHead.Finder.FindAll(" id in (" + string.Join(",", headList) + ") ");

            try
            {

                foreach (ARBillHead billhead in arbillheads)
                {
                    PLCacheManager.DataCache.FlushCache();
                    PLCacheManager.ObjectCache.FlushCache();
                    using (UFSoft.UBF.Business.ISession session = UFSoft.UBF.Business.Session.Open())
                    {

                        ARBillHead newBillHead = ARBillHead.Create();
                        ARBillHead cloneBillHead = (ARBillHead)billhead.Clone();
                        cloneBillHead.RedFlushStatus = U9.AR.Enums.RedFlushStatusEnum.Empty;
                        cloneBillHead.CopyTo(newBillHead);

                        newBillHead.DocNo = string.Empty;
                        newBillHead.SysState = ObjectState.Inserted;
                        newBillHead.SysVersion = 0;
                        //newBillHead.RedFlushBill = null;
                        newBillHead.RedFlushStatus = U9.AR.Enums.RedFlushStatusEnum.Empty;
                        newBillHead.DocStatus = BillStatusEnum.Opened;

                        //decimal taxamount = 0;
                        //decimal notaxamount = 0;
                        foreach (ARBillLine arbillline in billhead.ARBillLines.OfType<ARBillLine>())
                        {

                            ARBillLine cloneLine = (ARBillLine)arbillline.Clone();
                            ARBillLine newLine = ARBillLine.Create(newBillHead);
                            cloneLine.CopyTo(newLine);
                            newLine.ARBillHeadKey = newBillHead.Key;

                            newLine.SysState = ObjectState.Inserted;
                            newLine.SysVersion = 0;
                            newLine.IsRedFlush = false;
                            foreach (ARInstalment arinstalment in cloneLine.ARInstalments)
                            {

                                ARInstalment newARInstalment = ARInstalment.Create(newLine);
                                ARInstalment cloneARInstalment = (ARInstalment)arinstalment.Clone();
                                cloneARInstalment.CopyTo(newARInstalment);
                                newARInstalment.ARBillHeadKey = newBillHead.Key;
                                newARInstalment.ARBillLineKey = newLine.Key;
                                newARInstalment.ParentObjectKey = newLine.Key;

                            }



                            var shipline = shiplines.Where(p => p.ID == arbillline.SrcBillLineID).FirstOrDefault();
                            var t = listModifiedSoline.Where(p => p.SOLine.Equals(shipline.SOLineKey)).FirstOrDefault();

                            if (t != null)
                            {
                                //从销售变更单行中取出 核币OC和本币FC 金额 未税金额 税额
                                //核币
                                var modifylinelist = SOModifyLine.Finder.FindAll($"SOModify='{t.SOModifyKey.ID}' and SOLine={t.SOLine} ").OfType<SOModifyLine>();
                                string actax = modifylinelist.Where(x => (x.ModifyDataItem == "TaxMoneyAC" || x.ModifyDataItemName == "税额(核币)")).FirstOrDefault()?.NewValue;
                                string acnotax = modifylinelist.Where(x => (x.ModifyDataItem == "NetMoneyAC" || x.ModifyDataItemName == "无税金额(核币)")).FirstOrDefault()?.NewValue;
                                string acmon = modifylinelist.Where(x => (x.ModifyDataItem == "TotalMoneyAC" || x.ModifyDataItemName == "价税合计(核币)")).FirstOrDefault()?.NewValue;
                                //本币                                
                                string fctax = modifylinelist.Where(x => (x.ModifyDataItem == "TaxMoneyFunC" || x.ModifyDataItemName == "税额(本币)")).FirstOrDefault()?.NewValue;
                                string fcnotax = modifylinelist.Where(x => (x.ModifyDataItem == "NetMoneyFC" || x.ModifyDataItemName == "无税金额(本币)")).FirstOrDefault()?.NewValue;
                                string fcmon = modifylinelist.Where(x => (x.ModifyDataItem == "TotalMoneyFC" || x.ModifyDataItemName == "价税合计(本币)")).FirstOrDefault()?.NewValue;

                                if (shipline.IsPriceIncludeTax)
                                {

                                    newLine.TaxPrice = decimal.Parse(t.NewValue);
                                    newLine.NonTaxPrice = decimal.Round(newLine.TaxPrice / (1 + shipline.TaxRate), 2);
                                }
                                else
                                {
                                    newLine.NonTaxPrice = decimal.Parse(t.NewValue);
                                    newLine.TaxPrice = decimal.Round(newLine.NonTaxPrice * (1 + shipline.TaxRate), 2);

                                }

                                newLine.AROCMoney.GoodsTax = decimal.Parse(actax);
                                newLine.AROCMoney.Tax = decimal.Parse(actax);
                                newLine.AROCMoney.NonTax = decimal.Parse(acnotax);
                                newLine.AROCMoney.TotalMoney = decimal.Parse(acmon);

                                newLine.ARFCMoney.GoodsTax = decimal.Parse(fctax);
                                newLine.ARFCMoney.Tax = decimal.Parse(fctax);
                                newLine.ARFCMoney.NonTax = decimal.Parse(fcnotax);
                                newLine.ARFCMoney.TotalMoney = decimal.Parse(fcmon);

                                //税和税明细不符合                                
                                newLine.AROCMoneyBalance.GoodsTax = newLine.AROCMoney.GoodsTax;
                                newLine.AROCMoneyBalance.Tax = newLine.AROCMoney.Tax;
                                newLine.AROCMoneyBalance.NonTax = newLine.AROCMoney.NonTax;
                                newLine.AROCMoneyBalance.TotalMoney = newLine.AROCMoney.GoodsTax;
                                newLine.ARFCMoneyBalance.NonTax = newLine.ARFCMoney.NonTax;
                                newLine.ARFCMoneyBalance.Tax = newLine.ARFCMoney.Tax;
                                newLine.ARFCMoneyBalance.GoodsTax = newLine.ARFCMoney.GoodsTax;
                                newLine.ARFCMoneyBalance.TotalMoney = newLine.ARFCMoney.TotalMoney;
                                newLine.CfmedARGoodsTax.OCMoney = newLine.AROCMoney.Tax;
                                newLine.CfmedARGoodsTax.FCMoney = newLine.ARFCMoney.Tax;
                                newLine.CanInvoiceOCMoney.NonTax = newLine.AROCMoney.NonTax;
                                newLine.CanInvoiceOCMoney.Tax = newLine.AROCMoney.Tax;
                                newLine.CanInvoiceOCMoney.GoodsTax = newLine.AROCMoney.GoodsTax;
                                newLine.CanInvoiceOCMoney.TotalMoney = newLine.AROCMoney.TotalMoney;
                                newLine.CanInvoiceFCMoney.NonTax = newLine.ARFCMoney.NonTax;
                                newLine.CanInvoiceFCMoney.Tax = newLine.ARFCMoney.Tax;
                                newLine.CanInvoiceFCMoney.GoodsTax = newLine.ARFCMoney.GoodsTax;
                                newLine.CanInvoiceFCMoney.TotalMoney = newLine.ARFCMoney.TotalMoney;
                                newLine.CanInvoiceFCMoney.TotalMoney = newLine.ARFCMoney.TotalMoney;
                                //税明细
                                foreach (ARBillTaxDetail taxDetail in cloneLine.ARBillTaxDetails)
                                {

                                    ARBillTaxDetail newARTaxDetail = ARBillTaxDetail.Create(newLine);
                                    ARBillTaxDetail cloneARTaxDetail = (ARBillTaxDetail)taxDetail.Clone();
                                    cloneARTaxDetail.CopyTo(newARTaxDetail);
                                    newARTaxDetail.ARBillLineKey = newLine.Key;
                                    newARTaxDetail.ParentObjectKey = newLine.Key;
                                    newARTaxDetail.LineNum = newLine.LineNum;
                                    newARTaxDetail.TaxMoney.FCMoney = newLine.ARFCMoney.Tax;
                                    newARTaxDetail.TaxMoney.OCMoney = newLine.AROCMoney.Tax;
                                    //确认应收税额
                                    newARTaxDetail.CfmARTax.FCMoney = newLine.CfmedARGoodsTax.FCMoney;
                                    newARTaxDetail.CfmARTax.OCMoney = newLine.CfmedARGoodsTax.OCMoney;
                                    //未确认应收税额
                                    newARTaxDetail.UnCfmARTax.FCMoney = 0;
                                    newARTaxDetail.UnCfmARTax.OCMoney = 0;
                                    //未确认收入税额
                                    newARTaxDetail.UnRCTax.FCMoney = 0;
                                    newARTaxDetail.UnRCTax.OCMoney = 0;
                                }

                                //taxamount += decimal.Parse(fctax);
                                //notaxamount += decimal.Parse(fcnotax);

                            }
                            else
                            {
                                //throw new Exception("应付单未匹配到对应销售变更单行信息");
                                //源单已被红冲 销售变更单未变更的行  则直接复制源行即可 2025年4月10日
                            }
                        }
                        //decimal tax = taxamount - notaxamount;
                        //newBillHead.AROCMoney.TotalMoney = taxamount;
                        //newBillHead.AROCMoney.NonTax = notaxamount;
                        //newBillHead.AROCMoney.Tax = tax;
                        //newBillHead.AROCMoney.GoodsTax = tax;

                        //newBillHead.AROCMoneyBalance.TotalMoney = taxamount;
                        //newBillHead.AROCMoneyBalance.NonTax = notaxamount;
                        //newBillHead.AROCMoneyBalance.Tax = tax;
                        //newBillHead.AROCMoneyBalance.GoodsTax = tax;

                        //newBillHead.ARFCMoney.TotalMoney = taxamount;
                        //newBillHead.ARFCMoney.NonTax = notaxamount;
                        //newBillHead.ARFCMoney.Tax = tax;
                        //newBillHead.ARFCMoney.GoodsTax = tax;


                        //newBillHead.CanInvoiceOCMoney.TotalMoney = taxamount;
                        //newBillHead.CanInvoiceOCMoney.NonTax = notaxamount;
                        //newBillHead.CanInvoiceOCMoney.Tax = tax;
                        //newBillHead.CanInvoiceOCMoney.GoodsTax = tax;

                        //newBillHead.CanInvoiceFCMoney.TotalMoney = taxamount;
                        //newBillHead.CanInvoiceFCMoney.NonTax = notaxamount;
                        //newBillHead.CanInvoiceFCMoney.Tax = tax;
                        //newBillHead.CanInvoiceFCMoney.GoodsTax = tax;

                        session.Commit();
                    }
                }

            }
            catch (Exception ex)
            {
                LoggerManager.GetLogger("AdjustmentAR").Error(ex.Message + "\r\n" + ex.StackTrace);
                throw ex;
            }


        }
    }
}
